// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import QMLFile 1.0
import "Android"
import "Components"
import "./js/ThemeInfo.js" as ThemeInfo
import "./js/Database.js" as Database

Rectangle {
    width: 720
    height: 640

    Component.onCompleted: {
        var path = Database.readData("THEMEPATH");
        if(path === undefined){
            path = "";
        }
        console.debug("load:" + path);

        _themeSetting.rootPath = path;
        if(_themeSetting.rootPath.length > 0){
            //リスト更新
            updateThemeList(_themeSetting.rootPath);
            //内容更新
            _themeSetting.update();
        }else{
            //ない
            var dir = _file.getExistingDirectory("JcThemeSimulator : テーマフォルダ選択(/hoge/hoge/mythemeを選んでください。)", "");
            //変更
            changeThemePath(dir);
        }
    }
    Component.onDestruction: {
        Database.saveData("THEMEPATH", _themeSetting.rootPath);
    }

    // テーマフォルダの位置変更
    function changeThemePath(path){
        if(path.length < 1){
        }else{
            while(path.indexOf("\\") > 0){
                path = path.replace("\\", "/");
            }
            var pos = path.lastIndexOf("/");
            if(pos === path.length){
                path = path.substring(0, path.length - 1);
            }
            _themeSetting.rootPath = path;

            //リスト更新
            updateThemeList(path);
            //内容更新
            _themeSetting.update();
        }
    }

    //テーマリスト更新
    function updateThemeList(path){
        var temp = _file.getEntryList(path);
        var list = temp.split("\\");
        //消す
        _themeList.model.clear();
        //内容を登録
        for(var i=0; i<list.length; i++){
            if(list[i] === "." || list[i] === ".."){
            }else{
                _themeList.model.append({"_title": list[i]});
            }
        }
        //初期テーマ名を設定
        if(_themeList.count > 0){
            _themeSetting.themeName = _themeList.model.get(0)._title;
        }
    }

    Item{
        id: _themeSetting

        property string rootPath: ""                //テーマのルートパス（最後に/はいらない）
        property string themeName: ""               //選択してるテーマの名前（フォルダ名）

        function update(){
            var pathBase = rootPath + "/" + themeName + "/";

            _android.launcherPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_LAUNCHER"]
                                                     , "../Android/images/default/launcher_wallpaper.png");
            _android.lockScreenPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_LOCKSCREEN"]
                                                     , "../Android/images/default/lockscreen_wallpaper.png");

            _android.naviKeyBackPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NAVIKEY_BACK"]
                                                     , "../Android/images/default/ic_sysbar_back.png");
            _android.naviKeyExpandPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NAVIKEY_EXPAND"]
                                                     , "../Android/images/default/ic_sysbar_expand.png");
            _android.naviKeyHomePath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NAVIKEY_HOME"]
                                                     , "../Android/images/default/ic_sysbar_home.png");
            _android.naviKeyMenuPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NAVIKEY_MENU"]
                                                     , "../Android/images/default/ic_sysbar_menu.png");
            _android.naviKeyRecentPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NAVIKEY_RECENT"]
                                                     , "../Android/images/default/ic_sysbar_recent.png");

            _android.notificationItemNormalPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NOTIFICATION_ITEM_NORMAL"]
                                                     , "../Android/images/default/notification_item_background_00_normal.png");
            _android.notificationItemPressedPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_SCPATH_NOTIFICATION_ITEM_PRESSEDREENSHOT"]
                                                     , "../Android/images/default/notification_item_background_00_pressed.png");
            _android.notificationTrackingBgPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_NOTIFICATION_TRACKINGBG"]
                                                     , "../Android/images/default/notification_tracking_bg.png");

            _android.screenshotPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_SCREENSHOT"]
                                                     , "../Android/images/default/screenshot_frame.png");
            _android.wallpaperPath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_WALLPAPER"]
                                                     , "../Android/images/default/home_wallpaper.png");
            _android.recentFramePath = checkExists(pathBase + ThemeInfo.ITEM_PATH["PATH_RECENT_FRAME"]
                                                     , "../Android/images/default/my_frame.png");
        }

        // 存在チェック
        function checkExists(path, default_path){
            if(_file.exists(path)){
                return path;
            }else{
                return default_path;
            }
        }
    }

    QMLFile{
        id: _file
    }

    AndroidDesktop{
        id: _android
        clip: true
    }

    Column{
        id: _controlArea
        anchors.top: parent.top
        anchors.left: _android.right
        anchors.topMargin: 10
        anchors.leftMargin: 10
        spacing: 5

        Text{
            text: "操作ボタン:"
        }

        Column{
            spacing: 5

            Button{
                text: "  Lock  "
                onClicked: {
                    _android.screenLock(true);
                }
            }
            Button{
                text: " ScreenShot "
                onClicked: {
                    _android.takeScreenShot();
                }
            }
        }
    }

    Column{
        id: _selectArea
        anchors.top: parent.top
        anchors.left: _controlArea.right
        anchors.topMargin: 10
        anchors.leftMargin: 10
        spacing: 5

        Text{
            text: "現在のテーマ : " + _themeSetting.themeName
        }

        Text{
            text: "テーマ一覧 : "
        }

        Rectangle{
            width: 150
            height: 200
            border.color: "#dddddd"
            border.width: 1
            ListViewBase{
                id: _themeList
                anchors.fill: parent
                clip: true
                alwaysViewing: true
                scrollBarBaseColor: "#22000000"
                scrollBarColor: "#444444"
                model: ListModel{}
                delegate: Rectangle{
                    id: _delegate
                    width: _themeList.width-1
                    height: 30
                    border.color: "#dddddd"
                    border.width: 1
                    opacity: 0.8
                    Text{
                        anchors.centerIn: parent
                        text: _title
                    }
                    MouseArea{
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: {
                            _themeSetting.themeName = _title;
                            _themeSetting.update();
                        }
                        property color prev_color: "#ffffff"
                        onPressed: {
                            prev_color = _delegate.color;
                            _delegate.color = "#8888dd";
                        }
                        onReleased: {
                            _delegate.color = prev_color;
                        }
                        onEntered: {
                            _delegate.color = "#dd8888";
                        }
                        onExited: {
                            _delegate.color = "#ffffff";
                        }
                    }
                }
            }

        }

        Button{
            text: "テーマフォルダ選択..."
            onClicked: {
                var dir = _file.getExistingDirectory("テーマフォルダ選択", "");
                //変更
                changeThemePath(dir);
            }
        }
    }

}
